package com.dycloud.project.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.dycloud.project.domain.MeetingReceipt;
import com.dycloud.project.domain.Meetings;
import com.dycloud.project.dto.MeetingReceiptDTO;
import com.dycloud.project.exception.ServiceException;
import com.dycloud.project.mapper.MeetingReceiptMapper;
import com.dycloud.project.request.MeetingReceiptRequest;
import com.dycloud.project.service.IMeetingReceiptService;
import com.dycloud.project.service.IMeetingsService;
import com.dycloud.project.utils.DateUtils;
import com.dycloud.project.utils.SecurityUtils;
import com.dycloud.project.vo.MeetingPageVO;
import com.dycloud.project.vo.MeetingReceiptListVO;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author : renhang
 * @description MeetingReceiptServiceImpl
 * @date : 2025-07-03 15:57
 **/
@Service
public class MeetingReceiptServiceImpl implements IMeetingReceiptService {
    @Autowired
    private MeetingReceiptMapper meetingReceiptMapper;

    @Resource
    private IMeetingsService meetingService;

    /**
     * 查询会议回执
     *
     * @param id 会议回执主键
     * @return 会议回执
     */
    @Override
    public MeetingReceipt selectMeetingReceiptById(Long id) {
        return meetingReceiptMapper.selectMeetingReceiptById(id);
    }

    /**
     * 查询会议回执列表
     *
     * @param meetingReceipt 会议回执
     * @return 会议回执
     */
    @Override
    public List<MeetingReceiptListVO> selectMeetingReceiptList(MeetingReceipt meetingReceipt) {
        return meetingReceiptMapper.selectMeetingReceiptList(meetingReceipt);
    }

    /**
     * 新增会议回执
     *
     * @param meetingReceipt 会议回执
     * @return 结果
     */
    @Override
    public int insertMeetingReceipt(MeetingReceipt meetingReceipt) {
        meetingReceipt.setCreateTime(DateUtils.getNowDate());
        return meetingReceiptMapper.insertMeetingReceipt(meetingReceipt);
    }


    @Override
    public void write(MeetingReceiptRequest request) {
        MeetingReceipt meetingReceipt = meetingReceiptMapper.selectUserMeetingReceipt(request.getMeetingId(), SecurityUtils.getUserId());
        if (Objects.nonNull(meetingReceipt)) {
            throw new ServiceException("您已填写过该会议回执!请勿重复填写");
        }
        MeetingReceipt newReceipt = BeanUtil.copyProperties(request, MeetingReceipt.class);
        newReceipt.setUserId(SecurityUtils.getUserId());
        newReceipt.setCreateTime(new Date());
        newReceipt.setUpdateTime(new Date());
        newReceipt.setName(SecurityUtils.getLoginUser().getUser().getNickName());
        meetingReceiptMapper.insertMeetingReceipt(newReceipt);
    }

    @Override
    public List<MeetingReceiptListVO> selectUserMeetingReceiptList(MeetingReceipt meetingReceipt) {
        List<MeetingReceiptListVO> vos = meetingReceiptMapper.selectMeetingReceiptList(meetingReceipt);
        if (CollUtil.isEmpty(vos)) {
            return vos;
        }
        List<Long> list = vos.stream().map(MeetingReceiptListVO::getMeetingId).toList();
        List<Meetings> meetingsList = meetingService.selectMeetingByIds(list);
        List<MeetingPageVO> meetingPages = meetingService.meetingToPageVo(meetingsList);
        Map<Long, MeetingPageVO> meetingsMap = meetingPages.stream().collect(Collectors.toMap(MeetingPageVO::getId, v -> v));

        vos.forEach(item -> {
            item.setMeeting(meetingsMap.get(item.getMeetingId()));
        });
        return vos;
    }

    @Override
    public List<MeetingReceiptDTO> queryUserIsWrite(List<Long> meetingIds, Long userId) {
        return meetingReceiptMapper.selectUserIsWrite(meetingIds, userId);
    }
}
